<?php
/**
 * Dirox Framework
 * Copyright (C) 2005 - 2007 Dirox / Synexser
 * 
 * All rights reserved.
 */

function __autoload($classname)
{
	if (USE_LOWERCASE_FILENAMES)
	{
		$classname = strtolower($classname);
	}
	$filename               = $classname . ".php";
	$businessFileLocation   = APPLICATION_DIR . "/application/business/" . strtolower($filename);
	$controllerFileLocation = APPLICATION_DIR . "/application/controller/" .$filename;
	$classFileLocation      = APPLICATION_DIR . "/library/class/" . $filename;
	$drxClassFileLocation	= APPLICATION_DIR . "/library/class/DRX_" . $filename;

	if (file_exists($businessFileLocation)) {
		require_once($businessFileLocation);
	} else if (file_exists($controllerFileLocation)) {
		require_once($controllerFileLocation);
	} else if (file_exists($classFileLocation)) {
		require_once($classFileLocation);
	} else if (file_exists($drxClassFileLocation)) {
		require_once($drxClassFileLocation);
	} elseif (function_exists("DOMPDF_autoload"))
		DOMPDF_autoload($classname);
}
    
function cleanArray(&$arr, $value=null)
{
	$total = count($arr);
	for ($i = 0; $i < $total; $i++) {
		if ($arr[$i] == $value) {
			unset($arr[$i]);	
		}
	}
}

function redirect($url) {
	header("Location: " . $url);
	exit();
}

/**
 * Get Array Date Time array('day','month','year','hour','minute','second')
 * @example getDateArray("26/3/2007 03:23:27","d/m/y H:i:s")
 */
function getDateArray( $date_time, $format="d/m/y H:i", $seperator="/")
{
	$arr		= explode(" ",$date_time);
	$date		= $arr[0];
	if(count($arr)>1) $time			= $arr[1];

	$date_format					= substr($format,0,5);

	if( $date_format == 'd/m/y')
	{
		list($day,$month,$year)			= explode($seperator,$date);
	}
	else if( $date_format == 'm/d/y')
	{
		list($month,$day,$year)			= explode($seperator,$date);
	}
	else if($date_format == 'y/m/d')
	{
		list($year,$month,$day)			= explode($seperator,$date);
	}
	
	if(isset($time) && $time != '') list($hour,$minute)			= explode(":",$time); 
	
	if(isset($time) && $time != '')
	{
		return array("day"=>$day,"month"=>$month,"year"=>$year,"hour"=>$hour,"minute"=>$minute);
	}
	return array("day"=>$day,"month"=>$month,"year"=>$year);
//	$seperator_arr		= array('.','-');
//	
//	$arr		= explode(" ",$date);	
//	$date		= $arr[0];
//	
//	if( count($arr) > 1 )
//	{		
//		$time		= $arr[1];
//	}
//	$result		= array();
//			
//	$arrDate	= explode($seperator,$date);
//	$arr		= explode($seperator,$format);
//	
//	if( count($arrDate) <= 1 )
//	{
//		for( $i = 0 ; $i < count($seperator_arr) ; $i++ )
//		{
//			$arrDate	= explode($seperator_arr[$i]);
//			if( count($arrDate) > 1)
//			{
//				$seperator	= $seperator_arr[$i];
//				break;
//			}
//		}
//	}
//	
//	for( $j = 0 ; $j < count($arr) ; $j++ )
//	{
//		if( strpos($arr[$j],"d") === false )
//		{					
//		}else
//		{					
//			$result['day']	= $arrDate[$j];	
//		}
//		if( strpos($arr[$j],"m") === false )
//		{					
//		}else
//		{
//			$result['month']	= $arrDate[$j];	
//		}
//		if( strpos($arr[$j],"y") === false )
//		{					
//		}else
//		{
//			$result['year']	= $arrDate[$j];					
//		}				
//	}
//	
//	if( $time != '')
//	{
//		$arrTime		= explode(":",$time);
//		$arr	= explode(":",$format);
//		
//		if(count($arr)>1)
//		{
//			for( $j = 0 ; $j < count($arr) ; $j++ )
//			{
//				if( strpos($arr[$j],"H") === false )
//				{					
//				}else
//				{					
//					$result['hour']	= $arrTime[$j];	
//				}
//				if( strpos($arr[$j],"i") === false )
//				{					
//				}else
//				{
//					$result['minute']	= $arrTime[$j];	
//				}
//				if( strpos($arr[$j],"s") === false )
//				{					
//				}else
//				{
//					$result['second']	= $arrTime[$j];					
//				}	
//			}
//		}
//	}
//	return $result;

	
}

/**
	 * Get all element relate to paging
	 *
	 * @param array $profiles
	 * @param current page $page
	 * @param item per page $itemPerPage
	 * @return array
	 */
function pagingProfile( $arrMain, $itemPerPage, &$args,$page = 1 )
{		
		
	$tmp_arr = array();
	$result = array();
		
	$endIndex = ($page * $itemPerPage ) - 1;
	$startIndex = $endIndex - $itemPerPage + 1;
	
	$totalPage = 1;
	if( ( count($arrMain)%$itemPerPage ) == 0 )
	{
		$totalPage = count($arrMain)/$itemPerPage;
	}else{
		$totalPage = floor(count($arrMain)/$itemPerPage) + 1;
	}

	if( $endIndex < count($arrMain) )
	{

	}
	else {
		$endIndex = count($arrMain)-1;
	}	
	for( $i = $startIndex ; $i <= $endIndex ; $i++ )
	{
		$tmp_arr[] = $arrMain[$i];
	}

	$args["START_INDEX"] 	= $startIndex;
	$args["END_INDEX"]		= $endIndex;
	$args["TOTAL_PAGE"]		= $totalPage;
	$args["CURRENT_PAGE"]	= $page;
	$args['TOTAL_RECORDS']	= count($arrMain);

	return $tmp_arr;
}

/**
 * Convert DataObject To Array
 * @param DataObject $dbobject
 * @return array field=>value
 */
function convertToArray($dbobject)
{
//	$dbobject		= new DRX_DataObject;
	$result			= array();
	
	$dbobject->find();
	while( $dbobject->fetch() )
	{		
		array_push($result,$dbobject->toArray());
	}
	return $result;
}
